from HTMLParser import incomplete
from mysite.finance.models import BudgetItem, Frequency
from django.shortcuts import render_to_response, get_object_or_404
import locale

def index(request):
    balance_summary = VwBudgetSummaryBalance('Balance')

    income_list = []
    income_total = VwAggregate('')
    income_summary = VwBudgetSummary('Income')
    for i in BudgetItem.objects.filter(category__type=0):
        if(income_total.Name != i.category.name):
            income_total = VwAggregate(i.category.name)
        income_total.Add(i.frequency, i.amount)
        income_list.append(VwBudgetItem(i))
        income_summary.AddItem(i)
        balance_summary.AddItem(i)

    spending_list = []
    spending_total = VwAggregate('')
    spending_summary = VwBudgetSummary('Spending')
    for i in BudgetItem.objects.order_by('category', 'name').filter(category__type=1):
        if(spending_total.category != i.category):
            spending_total = VwAggregate(i.category)
            spending_list.append(spending_total)
        spending_total.Add(i.frequency, i.amount)
        spending_list.append(VwBudgetItem(i))
        spending_summary.AddItem(i)
        balance_summary.AddItem(i)

    return render_to_response('finance/index.html', 
        {
            'summary': [income_summary, spending_summary, balance_summary],
            'income_list': income_list,
            'spending_list': spending_list,
        })

def budgetItemView(request, budgetitem_id):
    b = get_object_or_404(BudgetItem, pk=budgetitem_id)
    return render_to_response('finance/budgetItemView.html', {'budgetItem': b})

class VwBudgetItem():

    def __init__(self, budgetItem):
        self.bi = budgetItem

    def Id(self):
        return self.bi.id

    def Name(self):
        return self.bi.name

    def Category(self):
        return self.bi.category

    def Frequency(self):
        return self.bi.frequency

    def Amount(self):
        return locale.currency(self.bi.amount, grouping=True)

    def Weekly(self):
        return locale.currency(self.bi.amount * self.bi.frequency.scalar / 52, grouping=True)

    def Monthly(self):
        return locale.currency(self.bi.amount * self.bi.frequency.scalar / 12, grouping=True)

    def Yearly(self):
        return locale.currency(self.bi.amount * self.bi.frequency.scalar, grouping=True)

    # is single item type
    def ItemType(self):
        return 0

class VwAggregate():
    category = None
    weekly = 0
    monthly = 0
    yearly = 0

    def __init__(self, category):
        self.category = category

    def Name(self):
        return self.category.name

    def Category(self):
        return self.category

    def Add(self, freq, amnt):
        self.weekly += CalcWeekly(freq, amnt)
        self.monthly += CalcMonthly(freq, amnt)
        self.yearly += CalcYearly(freq, amnt)

    def Weekly(self):
        return locale.currency(self.weekly, grouping=True)

    def Monthly(self):
        return locale.currency(self.monthly, grouping=True)

    def Yearly(self):
        return locale.currency(self.yearly, grouping=True)

    # is aggregate item type
    def ItemType(self):
        return 1


class VwBudgetSummaryBase():
    def __init__(self, name):
        self.name = name
        self.items = []

class VwBudgetSummary(VwBudgetSummaryBase):

    def AddItem(self, budgetItem):
        self.items.append(budgetItem)

    def Name(self):
        return self.name

    def WeeklyTotal(self):
        total = 0
        for i in self.items:
            total += CalcWeekly(i.frequency, i.amount)
        return locale.currency(total, grouping=True)

    def MonthlyTotal(self):
        total = 0
        for i in self.items:
            total += CalcMonthly(i.frequency, i.amount)
        return locale.currency(total, grouping=True)

    def YearlyTotal(self):
        total = 0
        for i in self.items:
            total += CalcYearly(i.frequency, i.amount)
        return locale.currency(total, grouping=True)

class VwBudgetSummaryBalance(VwBudgetSummary):

    def WeeklyTotal(self):
        total = 0
        for i in self.items:
            if i.category.type == 0:
                total += CalcWeekly(i.frequency, i.amount)
            else:
                total -= CalcWeekly(i.frequency, i.amount)
        return locale.currency(total, grouping=True)

    def MonthlyTotal(self):
        total = 0
        for i in self.items:
            if i.category.type == 0:
                total += CalcMonthly(i.frequency, i.amount)
            else:
                total -= CalcMonthly(i.frequency, i.amount)
        return locale.currency(total, grouping=True)

    def YearlyTotal(self):
        total = 0
        for i in self.items:
            if i.category.type == 0:
                total += CalcYearly(i.frequency, i.amount)
            else:
                total -= CalcYearly(i.frequency, i.amount)
        return locale.currency(total, grouping=True)



def budgetByMonthView(request, month):
    return render_to_response('finance/budgetByMonthView.html', {'month': month})

locale.setlocale(locale.LC_ALL, '')

def CalcWeekly(freq, amnt):
    return amnt * freq.scalar / 52

def CalcMonthly(freq, amnt):
    return amnt * freq.scalar / 12

def CalcYearly(freq, amnt):
    return amnt * freq.scalar
